package com.kerbores.meeting.biz.impl;

import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;

import com.kerbores.meeting.biz.IUserService;
import com.kerbores.meeting.dao.IUserDao;
import com.kerbores.meeting.domain.User;
import com.kerbores.utils.entries.Result;
import com.kerbores.utils.web.pager.Pager;

/**
 * @author Kerbores
 *
 *         Create at 2015年1月16日 上午3:43:26
 */
@IocBean
public class UserService implements IUserService {
	@Inject
	private IUserDao userDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.meeting.biz.IUserService#login(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public Result login(String userName, String pwd) {
		User check = userDao.findByCondition(Cnd.where("name", "=", userName));
		if (check == null) {
			return Result.fail("用户不存在!");
		}
		if (!Strings.equals(check.getPwd(), Lang.md5(pwd))) {
			return Result.fail("密码不正确!!");
		}
		return Result.success().addData("loginUser", check);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.meeting.biz.IUserService#listByPager(int, int)
	 */
	@Override
	public Pager<User> listByPager(int page, int pageSize) {
		Pager<User> pager = new Pager<User>(pageSize, page);
		pager.setEntities(userDao.searchByPage(null, page, pageSize));
		pager.setCount(userDao.countAll());
		return pager;
	}
}
